package com.devouty.mp3online.services;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;

import java.io.IOException;
import java.util.List;

import java.util.logging.LogRecord;

/**
 * Created by devouty on 15年7月31日.
 */
public class MusicUtil {

    private List<String> musics = null;
    private MediaPlayer mPlayer;
    private Context mContext;
    public static int index = 0;
    public  boolean isPlaying = false;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Intent intent = new Intent(MusicService.CAST_ACTION_UPDATE);
            intent.putExtra(MusicService.KEY_MUSIC_INDEX, index);
            intent.putExtra(MusicService.MUSIC_TIME_CURR, mPlayer.getCurrentPosition());
            intent.putExtra(MusicService.MUSIC_TIME_TOTAL, mPlayer.getDuration());

            mContext.sendBroadcast(intent);
            handler.sendEmptyMessageDelayed(1, 500);
        }
    };
    public MusicUtil(Context mContext, List<String> musicList) {
        mPlayer = new MediaPlayer();
        this.musics = musicList;
        this.mContext = mContext;

        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                next();
            }
        });
    }

    public void play() {
        //music is playing
        mPlayer.start();
        isPlaying = true;
        handler.sendEmptyMessage(1);
    }
    public void play(int index)
    {
        MusicUtil.index = index;
        if(musics!=null)
        {
            String music = musics.get(MusicUtil.index);
            try{
                mPlayer.reset();
                mPlayer.setDataSource(mContext, Uri.parse(music));
                mPlayer.prepare();
                mPlayer.start();

                isPlaying = true;

                handler.sendEmptyMessage(0);
            }catch (IllegalArgumentException e)
            {
                e.printStackTrace();

            }catch (SecurityException e)
            {
                e.printStackTrace();
            }catch (IllegalStateException e)
            {
                e.printStackTrace();
            }catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    public void stop()
    {
        if(mPlayer!=null)
        {
            mPlayer.stop();
            isPlaying =false;
        }
    }
    public void pause()
    {
        if(mPlayer!=null)
        {
            mPlayer.pause();
            isPlaying = false;
        }
    }
    public void next()
    {
        if(musics!=null)
        {
            if(index==musics.size()-1)
            {
                index = 0;
            }else
            {
                index++;

            }
            play(index);
        }
    }


    public void prev()
    {
        if(musics!=null)
        {
            if(index ==0)
            {
                index = musics.size()-1;
            }else
            {
                index--;
            }
            play(index);
        }
    }
}
